DELETE FROM TBL_CM_PRIORITY

INSERT TBL_CM_PRIORITY
SELECT A.CIF, A.CUS_NAME, A.BRANCH_ID, A.CUS_OPEN_DATE, A.CUST_TYPE_NAME, A.INDUSTRY_NAME, A.INDUSTRY_NAME_EN,A.DAO, A.DAO_NAME , B.TOI, C.TOI AS TOI2015, NULL AS NHOM_NO
--INTO TBL_CM_PRIORITY
FROM TBL_CUSTOMER A
LEFT JOIN TBL_TOI B ON A.CIF COLLATE DATABASE_DEFAULT=B.CIF
LEFT JOIN TBL_CUST_TOI_2016 C ON A.CIF COLLATE DATABASE_DEFAULT = C.CIF
WHERE A.CUST_TYPE_NAME <>'OTHERS'


UPDATE A
SET NHOM_NO = B.NHOM_NO
FROM TBL_CM_PRIORITY A, TBL_CM_LOAN B
WHERE A.CIF = B.CIF

--DELETE FROM TOI_2016_BY_MONTH
--drop table TOI_2016_BY_MONTH

insert toi_2016_by_month
select *  from server12.vudt1.[dbo].[NFI_TOI] where type = 'TOI' and yearmonth < '2017-01'



SELECT CIF,SUM(AMT) AS TOI
INTO TBL_CUST_TOI_2016
 FROM TOI_2016_BY_MONTH 
GROUP BY CIF

